//
//  JsWebviewNativeManager.m
//  LazyCat
//
//  Created by zhanshu on 15/9/16.
//  Copyright (c) 2015年 zhanshu. All rights reserved.
//

#import "JsWebviewNativeManager.h"

@implementation JsWebviewNativeManager

+ (BOOL)theRequestUrl:(NSString *)requestString viewController:(id)VC success:(Success)success {
    NSArray *components = [requestString componentsSeparatedByString:@"@"];//提交请求时候分割参数的分隔符
    if (components.count == 2) {
        NSArray *resultArr = [components[1] componentsSeparatedByString:@":"];
        if ([resultArr count] > 1 && [(NSString *)[resultArr objectAtIndex:0] isEqualToString:@"lanmao_ios"]) {
            //过滤请求是否是我们需要的.不需要的请求不进入条件
            NSString *actionStr = [resultArr objectAtIndex:1];
            if([actionStr isEqualToString:@"opencart"]){
                //通知跳转至原生界面
                success(SendToCart,@"3");
                [(UIViewController*)VC dismissViewControllerAnimated:YES completion:^{
                    
                }];
                [[AppData share].baseTabVC changeBottomBtnWithIndex:2];
            }else if ([actionStr isEqualToString:@"needlogin"]) {
                //凡是加入购物车必须先登录，如果未登录，提示登录
                [(BaseViewController*)VC goLoginView];
                success(SendNeedLogin,@"3");
            }else if ([actionStr isEqualToString:@"cartcount"]) {
                // //通知原生界面，购物车内数量
                [[AppData share].baseTabVC theShoppingCartCount:resultArr[2]];
                success(SendCartCount,@"3");
            }else if ([actionStr isEqualToString:@"sendToProduct"]) {
                //在懒猫超市，点击产品详情，打开产品详情的原生界面
                success(SendToProduct,@"3");
                 NSString *str2 = [Utils URLDecodedString:resultArr[2]];
                NSArray *productMess = [str2 componentsSeparatedByString:@"^"];
                Html5ViewController *html5VC = [[Html5ViewController alloc] init];
                html5VC.doDetailType = YES;
                html5VC.titleStr = productMess[1];
                html5VC.requestUrl = [NSString stringWithFormat:@"http://%@",productMess[2]];
                html5VC.shareImgUrl = [NSString stringWithFormat:@"http://%@",productMess[3]];
                [[(BaseViewController*)VC navigationController] pushViewController:html5VC animated:YES];
            }else if ([actionStr isEqualToString:@"sendToLanmao"]) {
                //购物车页面，点击去逛逛，需要跳转至app中“懒猫超市”的交互
                success(SendToCart,@"3");
                [[AppData share].baseTabVC changeBottomBtnWithIndex:1];
            }else if ([actionStr isEqualToString:@"needlogin"]) {
                //在忘记密码，修改密码成功后，需要让程序跳转至原生登录对话框.先关闭当前页面  然后弹出登陆页面
                if ([(BaseViewController*)VC navigationController].viewControllers) {
                    [[(BaseViewController*)VC navigationController] popViewControllerAnimated:NO];
                }else {
                    [(BaseViewController*)VC dismissViewControllerAnimated:NO completion:^{
                        
                    }];
                }
                [[AppData share].baseTabVC goLoginView];
            }else if ([actionStr isEqualToString:@"sendToPay"]) {
                //购物车内，下完订单，打开支付界面，需要打开一个原生界面，来加载支付过程，不在购物车内跳转
                Html5ViewController *html5VC = [[Html5ViewController alloc] init];
                
//                html5VC.doDetailType = YES; // 隐藏分享按钮
                html5VC.titleStr = @"结算";
                html5VC.requestUrl = [NSString stringWithFormat:@"http://%@",resultArr[2]];
                [[(BaseViewController*)VC navigationController] pushViewController:html5VC animated:YES];
                //提交成功后 置空购物车数量
                [[AppData share].baseTabVC theShoppingCartCount:@"0"];
                
            }
            return NO;
        }
    }
    return YES;
}

@end
